/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package za.co.pas.spaceandtime.factory;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.PersistenceUnit;
import org.apache.log4j.Logger;

/**
 *
 * @author alabuschagne
 */
public class EMFFactory
{

    @PersistenceUnit
    private EntityManagerFactory emf;
    private static final EMFFactory singleton = new EMFFactory();
    private static Logger logger = Logger.getLogger(EMFFactory.class);
    private Properties properties;
    private EMFFactory()
    {
    }

    public static EMFFactory getInstance()
    {
        return singleton;
    }

    private void searchForFile()
    {
        FileOutputStream fos = null;
        try
        {
            File file = new File("YouAreHere.txt");
            fos = new FileOutputStream(file);
            fos.write(0);
        }
        catch (FileNotFoundException ex)
        {
            logger.error(ex, ex);
        }
        catch (IOException ex)
        {
            logger.error(ex, ex);
        }
        finally
        {
            try
            {
                fos.close();
            }
            catch (IOException ex)
            {
                logger.error(ex, ex);
            }
        }

    }
    /**
     * @TODO read this from properties
     * @return
     */
    public EntityManagerFactory getEntityManagerFactory() //throws IOException
    {
        if(properties == null)
        {
            //searchForFile();
            properties = new Properties();
            File file = new File("SpaceAndTimePU.properties");
            if(file.exists())
            {
                FileInputStream fis = null;
                try
                {
                    fis = new FileInputStream(file);
                    properties.load(fis);
                }
                catch (FileNotFoundException ex)
                {
                    logger.error(ex, ex);
                }
                catch (IOException ex)
                {
                    logger.error(ex, ex);
                }
                finally
                {
                    try
                    {
                        fis.close();
                    }
                    catch (IOException ex)
                    {
                        logger.error(ex, ex);
                    }
                }
            }
        }
        if (emf == null)
        {
            String s = properties.getProperty("PU", "WrongPU");
            try
            {
                emf = Persistence.createEntityManagerFactory(s);//"SpaceAndTimeLibPU");
            }
            catch(Exception ex)
            {
                logger.error(ex, ex);
            }
        }
        logger.debug("factory created");
        return emf;
    }

    public void closeEmf()
    {
        if (emf.isOpen() || emf != null)
        {
            emf.close();
        }
        emf = null;
        logger.debug("EMF closed");
    }
}
